package com.dionysun.graphqlkickstart.resolvers;

import com.coxautodev.graphql.tools.GraphQLQueryResolver;
import com.dionysun.graphqlkickstart.dao.ArticleRepository;
import com.dionysun.graphqlkickstart.dao.UserRepository;
import com.dionysun.graphqlkickstart.entity.Article;
import com.dionysun.graphqlkickstart.entity.User;
import com.dionysun.graphqlkickstart.enums.UserTypeEnum;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class QueryResolver implements GraphQLQueryResolver {

    private final UserRepository userRepository;
    private final ArticleRepository articleRepository;

    public QueryResolver(UserRepository userRepository, ArticleRepository articleRepository) {
        this.userRepository = userRepository;
        this.articleRepository = articleRepository;
    }

    public Article article(String title) {
        return Article.builder().id(12L).title(title).build();
    }

    public Article article2(String title, String content) {
        return Article.builder().id(12L).title(title).content(content).build();
    }

    public User user(String nickname) {
        return User.builder().id(12L).nickname(nickname)
            .mail("123")
            .createTime(new Date())
            .updateTime(new Date())
            .issueTime(new Date())
            .userTypeEnum(UserTypeEnum.ADMIN).build();
    }

    public User getOne(User user) {
        return user;
    }

    public User userOne(Long id) {
        return userRepository.findById(id).orElse(null);
    }

    public List<User> users() {
        return userRepository.findAll();
    }

    public List<User> getUserList(List<String> idList) {
        List<User> list = new ArrayList<>();
        for (long i = 0; i < 10; i++) {
            list.add(User.builder().id(i).nickname("nickname_" + i).build());
        }
        return list;
    }

    public User few(Long id, String nickname, String mail, String password, String description) {
        return User.builder().id(id).nickname(nickname).mail(mail).password(password).description(description).build();
    }

    public User simpleUser(User user) {
        return user;
    }

}
